package com.hnchances.wjb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hnchances.wjb.entity.Role;
import com.hnchances.wjb.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import javax.annotation.Resource;
import java.util.List;

@Mapper
@Resource
public interface UserMapper extends BaseMapper<User>{
//    SELECT p.id, p.permission, r.role FROM user u, roles r, users_roles ur, permissions p, roles_permissions rp WHERE u.userName = '王五' AND u.id = ur.user_id AND r.id = ur.role_id AND r.id = rp.role_id AND p.id = rp.permission_id;
    //多表查询 查找用户的权限
   @Select(" SELECT p.id, p.permission, r.role"+" FROM user u, roles r, users_roles ur, permissions p, roles_permissions rp"+" WHERE u.userName = #{username}"+" AND u.id = ur.user_id AND r.id = ur.role_id AND r.id = rp.role_id AND p.id = rp.permission_id")
   List<Role> queryByUsername(@Param("username") String username);

   //设置用户与角色的关系表
   @Insert("INSERT INTO `db_people`.`users_roles` (`user_id`,`role_id`)"+" VALUES (#{userId},#{roleId})")
    Integer insertRoles(@Param("userId") Integer userId,@Param("roleId")Integer roleId);


    @Select("SELECT t_user.user_name " +
            " FROM t_blog, t_user " +
            " WHERE t_blog.id = #{id} " +
            "     AND t_blog.user_id = t_user.id")
    User findById(@Param("id") int id);

}
